
package net.atremmedia.gwt.actionjava.examples.bounds.samples;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import net.atremmedia.gwt.actionjava.examples.bounds.samples.faces.Face;
import net.atremmedia.gwt.actionjava.examples.bounds.samples.faces.Face1;
import net.atremmedia.gwt.actionjava.examples.bounds.samples.faces.Face2;
import net.atremmedia.gwt.actionjava.examples.bounds.samples.faces.Face3;
import net.atremmedia.gwt.actionjava.examples.bounds.samples.faces.Face4;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.display.Graphics;
import actionjava.display.Shape;
import actionjava.display.Sprite;
import actionjava.geom.Color;
import com.allen_sauer.gwt.log.client.Log;

public class DrawGraphicsBounds3 extends AJExample
{
	/*
	 * Download FLA source files:
	 * http://www.actionjava.com/fla.zip
	 */

	public static final String NAME = "Draw Graphics bounds 3";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-bounds/src/main/java/net/atremmedia/gwt/actionjava/examples/bounds/samples/DrawGraphicsBounds3.java";

	public static final double MARGIN = 50;

	private int numDots = 10;
	private Sprite container;
	private Face face1;
	private Face face2;
	private Face face3;
	private Face face4;

	public DrawGraphicsBounds3(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	@Override
	public void run()
	{
		container = new Sprite();
		addChild(container);

		face1 = new Face1();
		face1.setX(0);
		face1.setY(0);
		container.addChild(face1);
		container.addChild(drawBounds(face1.getX(), face1.getY(), face1.getWidth(), face1.getHeight(), Color.RED.getHexValue()));

		face2 = new Face2();
		face2.setX(200);
		face2.setY(0);
		container.addChild(face2);
		container.addChild(drawBounds(face2.getX(), face2.getY(), face2.getWidth(), face2.getHeight(), Color.RED.getHexValue()));

		face3 = new Face3();
		face3.setX(0);
		face3.setY(200);
		container.addChild(face3);
		container.addChild(drawBounds(face3.getX(), face3.getY(), face3.getWidth(), face3.getHeight(), Color.RED.getHexValue()));

		face4 = new Face4();
		face4.setX(200);
		face4.setY(200);
		container.addChild(face4);
		container.addChild(drawBounds(face4.getX(), face4.getY(), face4.getWidth(), face4.getHeight(), Color.RED.getHexValue()));

		addRandomDots();

		container.setX((getWidth() / 2) - (container.getWidth() / 2));
		container.setY((getHeight() / 2) - (container.getHeight() / 2));
		addChild(drawBounds(container.getX(), container.getY(), container.getWidth(), container.getHeight(), Color.BLUE.getHexValue()));

		Log.debug("x: " + container.getX() + " y: " + container.getY() + " w: " + container.getWidth() + " h: " + container.getHeight());
	}

	private Shape drawBounds(double x, double y, double w, double h, String color)
	{
		Shape rect = new Shape();
		Graphics graph = rect.getGraphics();
		graph.beginStroke(color);
		graph.drawRect(x, y, w, h);
		graph.endStroke();

		return rect;
	}

	private void addRandomDots()
	{
		Shape dot;
		for(int i = 0;i < numDots;i++) {
			dot = new Shape();
			dot.getGraphics().beginFill(getRandomColor()).drawCircle(3, 3, 6).endFill();
			dot.setX(getRandomNumber(MARGIN, (getWidth() - MARGIN)));
			dot.setY(getRandomNumber(MARGIN, (getHeight() - 100)));
			container.addChild(dot);
		}
	}
}
